﻿/*===================================================
* 类名称: ABLoadHelper
* 类描述:
* 创建人: oream
* 创建时间: 2017/12/24 18:09:41
* 修改人: 
* 修改时间:
* 版本： @version 1.0
=====================================================*/

using System;
using System.IO;
using UnityEngine;
using AB = Model.ResourcesManager;
namespace Model
{
    public static class ABHepler
    {

        static GameObject loadResourcesUpdataView = null;

        /// <summary>
        /// 关闭加载资源更新界面
        /// </summary>
        public static void CloseLoadResourcesUpdataView()
        {
            if (loadResourcesUpdataView != null)
            {
                loadResourcesUpdataView.SetActive(false);
            }
        }

        /// <summary>
        /// 加载资源更新界面
        /// </summary>
        /// <returns></returns>
        private static GameObject LoadResourcesUpdataView()
        {
            GameObject go = null;
            string path = PathHelper.AppHotfixResPath;
            if (File.Exists(path + @"gameupdateview.assetbundle") && (File.Exists(path + PathHelper.GetCurrentPlatform())))
            {
                AB.Instance.LoadOneBundle(PathHelper.GetCurrentPlatform());
                AB.AssetBundleManifestObject = AB.Instance.GetAsset<AssetBundleManifest>(PathHelper.GetCurrentPlatform(), "AssetBundleManifest");
                AB.Instance.LoadBundle("gameupdateview.assetbundle");
                go = AB.Instance.GetAsset<GameObject>("gameupdateview.assetbundle", "DownloadCanvas");
            }
            if (go == null)
            {
                go = Resources.Load<GameObject>("DownloadCanvas");
            }
            return go;

        }
        /// <summary>
        /// 更细服务器AB包
        /// </summary>
        /// <param name="resServerUrl"></param>
        /// <returns></returns>
        public static void DownloadBundle(string resServerUrl, Action callback)
        {

            if (!Defines.IsAsync)
            {
#if UNITY_EDITOR
                callback();
#endif
            }
            else
            {
                loadResourcesUpdataView = GameObject.Instantiate<GameObject>(LoadResourcesUpdataView());
                AssetBundleDownloadManager.Instance.Awake(resServerUrl);
                AssetBundleDownloadManager.Instance.OnSuccess = () =>
                {
                    AB.Instance.LoadOneBundle(PathHelper.GetCurrentPlatform());
                    AssetBundleManifest abm = AB.Instance.GetAsset<AssetBundleManifest>(PathHelper.GetCurrentPlatform(), "AssetBundleManifest"); // 获取主包清单文件
                    AB.AssetBundleManifestObject = abm;
                    callback();
                };
            }
        }
    }
}
